﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Nop.Admin.Models.WindAlert;
using Nop.Core;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Security;
using Nop.Services.WindAlert;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;

namespace Nop.Admin.Controllers
{
    public partial class WindDataController : BaseAdminController
    {
        private readonly IWindDataService _windDataService;
        private readonly ISpotService _spotService;
        private readonly IWorkContext _workContext;
        private readonly ILocalizationService _localizationService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IPermissionService _permissionService;

        public WindDataController(IWindDataService windDataService,ISpotService SpotService, IWorkContext workContext,
            ILocalizationService localizationService, IDateTimeHelper dateTimeHelper,
            IPermissionService permissionService)
        {
            this._windDataService = windDataService;
            this._spotService = SpotService;
            this._workContext = workContext;
            this._localizationService = localizationService;
            this._dateTimeHelper = dateTimeHelper;
            this._permissionService = permissionService;
        }

        #region methods
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult List()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();
            var model = new WindDataListModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult WindDataList(DataSourceRequest command, WindDataListModel model)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            DateTime? createdOnFromValue = (model.CreatedOnFrom == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnFrom.Value, _dateTimeHelper.CurrentTimeZone);

            DateTime? createdToFromValue = (model.CreatedOnTo == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.CreatedOnTo.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1);


            var WindDataItems = _windDataService.GetAllWindData(createdOnFromValue, createdToFromValue, command.Page - 1, command.PageSize);
            var gridModel = new DataSourceResult
            {
                Data = WindDataItems.Select(x =>
                {
                    return new WindDataModel()
                    {
                        Id = x.Id,
                        CreatedOnUtc  = x.CreatedOnUtc,
                        StationTime = x.StationTime,
                        StationAdjustedTime = x.StationAdjustedTime,
                        WindSpeed  = x.WindSpeed,
                        WindGust = x.WindGust,
                        WindSpeedAgv = x.WindSpeedAgv,
                        WindDirection = x.WindDirection,
                        StationName = _spotService.GetSpotById((int)x.SpotId).Name,
                    };
                }),
                Total = WindDataItems.TotalCount
            };
            return new JsonResult
            {
                Data = gridModel
            };
        }
        
        [HttpPost, ActionName("List")]
        [FormValueRequired("clearall")]
        public ActionResult ClearAll()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            _windDataService.ClearAll();

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Cleared"));
            return RedirectToAction("List");
        }

        public ActionResult View(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var windData= _windDataService.GetWindDataById(id);
            if (windData == null)
                //No windData found with the specified id
                return RedirectToAction("List");

            var model = new WindDataModel()
            {
                Id = windData.Id,
                CreatedOnUtc = windData.CreatedOnUtc,
                StationTime = windData.StationTime,
                StationAdjustedTime = windData.StationAdjustedTime,
                WindSpeed = windData.WindSpeed,
                WindGust = windData.WindGust,
                WindSpeedAgv = windData.WindSpeedAgv,
                WindDirection = windData.WindDirection,
                StationName = _spotService.GetSpotById((int)windData.SpotId).Name,
            };

            return View(model);
        }

        [HttpPost]
       public ActionResult Delete(int id)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            var windData = _windDataService.GetWindDataById(id);
            if (windData == null)
                //No windata found with the specified id
                return RedirectToAction("List");

            _windDataService.DeleteWindData(windData);

            SuccessNotification(_localizationService.GetResource("Admin.System.Log.Deleted"));
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult DeleteSelected(ICollection<int> selectedIds)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageSystemLog))
                return AccessDeniedView();

            if (selectedIds != null)
            {
                var windDataItems = _windDataService.GetWindDataByIds(selectedIds.ToArray());
                foreach (var windDataItem in windDataItems)
                    _windDataService.DeleteWindData(windDataItem);
            }

            return Json(new { Result = true});
        }
        #endregion

        
    }
}